home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / slist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  4.0 KB  |  195 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/slist.c 1.2 1996/02/24 14:53:54 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Handle lists of strings.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the debugging information from this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "datatype.h"
  25. #include "cxref.h"
  26.  
  27. /*++++++++++++++++++++++++++++++++++++++
  28.   Called to initialise a new string list.
  29.  
  30.   StringList* sl The string list to initialise.
  31.   ++++++++++++++++++++++++++++++++++++++*/
  32.  
  33. void InitStringList(StringList* sl)
  34. {
  35. #if DEBUG
  36.  printf("#Slist.c# Initialise string list\n");
  37. #endif
  38.  
  39.  sl->n=0;
  40.  sl->s=NULL;
  41. }
  42.  
  43.  
  44. /*++++++++++++++++++++++++++++++++++++++
  45.   Called to initialise a new string list 2.
  46.  
  47.   StringList2* sl The string list 2 to initialise.
  48.   ++++++++++++++++++++++++++++++++++++++*/
  49.  
  50. void InitStringList2(StringList2* sl)
  51. {
  52. #if DEBUG
  53.  printf("#Slist.c# Initialise string list 2\n");
  54. #endif
  55.  
  56.  sl->n=0;
  57.  sl->s1=NULL;
  58.  sl->s2=NULL;
  59. }
  60.  
  61.  
  62. /*++++++++++++++++++++++++++++++++++++++
  63.   Add a string to the string list, the list stores a Malloced copy of str.
  64.  
  65.   StringList* sl The string list to add to.
  66.  
  67.   char* str The string to add.
  68.  
  69.   int alphalist If true then the list is sorted into alphabetical order.
  70.   ++++++++++++++++++++++++++++++++++++++*/
  71.  
  72. void AddToStringList(StringList* sl,char* str,int alphalist)
  73. {
  74.  int i;
  75.  
  76. #if DEBUG
  77.  printf("#Slist.c# Add string %s to the string list\n",str);
  78. #endif
  79.  
  80.  for(i=0;i<sl->n;i++)
  81.     if(!strcmp(str,sl->s[i]))
  82.        return;
  83.  
  84.  if(!sl->n)
  85.     sl->s=(char**)Malloc(8*sizeof(char*));
  86.  else
  87.     if(sl->n%8==0)
  88.        sl->s=(char**)Realloc(sl->s,(sl->n+8)*sizeof(char*));
  89.  
  90.  if(alphalist)
  91.    {
  92.     char *shuffle=NULL;
  93.     for(i=0;i<sl->n;i++)
  94.        if(shuffle)
  95.          {
  96.           char* temp=sl->s[i];
  97.           sl->s[i]=shuffle;
  98.           shuffle=temp;
  99.          }
  100.        else
  101.           if(strcmp(str,sl->s[i])<0)
  102.             {
  103.              shuffle=sl->s[i];
  104.              sl->s[i]=MallocString(str);
  105.             }
  106.  
  107.     if(!shuffle)
  108.        sl->s[sl->n]=MallocString(str);
  109.     else
  110.        sl->s[sl->n]=shuffle;
  111.    }
  112.  else
  113.     sl->s[sl->n]=MallocString(str);
  114.  
  115.  sl->n++;
  116. }
  117.  
  118.  
  119. /*++++++++++++++++++++++++++++++++++++++
  120.   Add a string to the string list 2, the list stores the values of the arguments.
  121.  
  122.   StringList2* sl The string list 2 to add to.
  123.  
  124.   char* str1 The first string to add.
  125.  
  126.   char* str2 The second string to add.
  127.   ++++++++++++++++++++++++++++++++++++++*/
  128.  
  129. void AddToStringList2(StringList2* sl,char* str1,char* str2)
  130. {
  131.  if(!sl->n)
  132.    {
  133.     sl->s1=(char**)Malloc(8*sizeof(char*));
  134.     sl->s2=(char**)Malloc(8*sizeof(char*));
  135.    }
  136.  else
  137.     if(sl->n%8==0)
  138.       {
  139.        sl->s1=(char**)Realloc(sl->s1,(sl->n+8)*sizeof(char*));
  140.        sl->s2=(char**)Realloc(sl->s2,(sl->n+8)*sizeof(char*));
  141.       }
  142.  
  143.  sl->s1[sl->n]=str1;
  144.  sl->s2[sl->n]=str2;
  145.  
  146.  sl->n++;
  147. }
  148.  
  149.  
  150. /*++++++++++++++++++++++++++++++++++++++
  151.   Delete a string list.
  152.  
  153.   StringList* sl The string list to delete.
  154.   ++++++++++++++++++++++++++++++++++++++*/
  155.  
  156. void DeleteStringList(StringList* sl)
  157. {
  158.  if(sl->n)
  159.    {
  160.     int i;
  161.  
  162.     for(i=0;i<sl->n;i++)
  163.        Free(sl->s[i]);
  164.  
  165.     Free(sl->s);
  166.    }
  167. }
  168.  
  169.  
  170. /*++++++++++++++++++++++++++++++++++++++
  171.   Delete a string list 2.
  172.  
  173.   StringList2* sl The string list 2 to delete.
  174.  
  175.   int free_them If this is true then the strings are to be freed.
  176.   ++++++++++++++++++++++++++++++++++++++*/
  177.  
  178. void DeleteStringList2(StringList2* sl,int free_them)
  179. {
  180.  if(sl->n)
  181.    {
  182.     int i;
  183.  
  184.     if(free_them)
  185.        for(i=0;i<sl->n;i++)
  186.          {
  187.           if(sl->s1[i])Free(sl->s1[i]);
  188.           if(sl->s2[i])Free(sl->s2[i]);
  189.          }
  190.  
  191.     Free(sl->s1);
  192.     Free(sl->s2);
  193.    }
  194. }
  195.